<template>
  <div>
    <div style="margin-top: 12px">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
<!--      <el-input style="width: 200px" placeholder="请输入昵称" suffix-icon="el-icon-user"  class="ml-5" v-model="nickname"></el-input>-->
<!--      <el-input style="width: 200px" placeholder="请输入昵称" suffix-icon="el-icon-message"  class="ml-5" v-model="email"></el-input>-->
<!--      <el-input style="width: 200px" placeholder="请输入地址" suffix-icon="el-icon-position"  class="ml-5" v-model="address"></el-input>-->
      <el-button style="margin-left: 5px" type="primary" @click="load">搜索</el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset">重置</el-button>
    </div>
    <div style="margin: 6px 0">
      <el-button type="primary" @click="handleAdd(null)">新增<i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm title="确认批量删除吗?" style="margin: 0 10px" @confirm="delBatch">
        <template #reference>
          <el-button type="danger" >批量删除<i class="el-icon-remove-outline"></i></el-button>
        </template>
      </el-popconfirm>
<!--      <el-upload action="http://localhost:9090/user/import" :show-file-list="false"-->
<!--                 accept=".xlsx" :on-success="handleExcelImportSuccess"-->
<!--                 style="display: inline-block">-->
<!--        <el-button type="primary">导入<i class="el-icon-bottom"></i></el-button>-->
<!--      </el-upload>-->
<!--      <el-button type="primary" @click="exp" style="margin-left: 10px">导出<i class="el-icon-top"></i></el-button>-->
    </div>
    <el-table :data="tableData" border stripe
              row-key="id"
              default-expand-all
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="id" width="80">
      </el-table-column>
      <el-table-column prop="name" label="名称" width="100">
      </el-table-column>
      <el-table-column prop="path" label="路径" width="120"/>
      <el-table-column prop="pagePath" label="页面路径" width="140"/>
      <el-table-column prop="icon" label="图标" width="80" align="center">
        <template slot-scope="scope">
          <i :class="scope.row.icon" style="font-size: 20px"/>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" width="140"></el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="200">
        <template #default = "scope">
          <el-button size="small" type="text" @click="handleAdd(scope.row.id)"
                     style="padding: 2px; color: #8c939d"
                     v-if="!scope.row.pid && !scope.row.path"
          ><i class="el-icon-menu" ></i>新增子菜单</el-button>
          <el-button size="small" type="text" @click="handleEdit(scope.row)"  style="padding: 2px">编辑</el-button>
          <el-popconfirm title="确认删除吗?" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="small"  type="text" style="padding: 2px">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="菜单信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="70px" >
        <el-form-item label="名称">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="路径">
          <el-input v-model="form.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="页面路径">
          <el-input v-model="form.pagePath" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <template slot-scope="scope">
            <el-select clearable v-model="form.icon" placeholder="请选择" style="width: 100%">
              <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value">
                <i :class="item.value"/> {{item.name}}
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false; form = []">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
.el-dialog__body{
  padding-bottom: 5px;
}
</style>

<script>

export default {
  name: "Menu",
  data() {
    return {
      tableData: [],
      total:0,
      name:"",
      form:{},
      dialogFormVisible: false,
      multipleSelection: [],
      options: []
    }
  },
  created() {
    this.load();
  },
  methods :{
    // 请求分页查询数据
    load(){
      this.request.get("menu",{
        params: {
          name: this.name,
        }
      }).then(res => {
        this.tableData = res.data
      })
      //请求图标数据
      this.request.get("/menu/icons").then(res => {
        this.options = res.data
      })
    },
    // 保存
    save() {
      console.log(this.form)
      this.request.post("/menu", this.form).then(res =>{
        if(res.data) {
          this.$message.success("保存成功")
        } else {
          this.$message.error("保存失败")
        }
        this.load()
      })
      this.dialogFormVisible = false;
      this.form = {};
    },
    //编辑
    handleEdit(row){
      this.form = JSON.parse(JSON.stringify(row)) //深拷贝
      this.form.id = row.id;
      this.dialogFormVisible = true;
    },
    // 删除
    handleDelete(id){
      this.request.delete("/menu/" + id).then(res => {
        if (res.data) {
          this.$message.success("删除成功"
          )
        } else {
          this.$message.error("删除失败")
        }
        this.load()
      })
    },
    //实现批量选择
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    //批量删除
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id) // 得到每个选择对象的 id
      console.log(ids)
      this.request.post("/menu/del/batch", ids).then(res => {
        if (res.data) {
          this.$message.success("批量删除成功")
        } else {
          this.$message.error("批量删除失败")
        }
        this.load()
      })
    },
    // 重新设置搜索框
    reset() {
      this.name = ""
      this.load()
    },
    // 打开新增表单
    handleAdd(pid) {
      this.dialogFormVisible = true
      this.form = {}
      if (pid) {
        this.form.pid = pid;
      }
      console.log(this.form)
    },

  }
}
</script>